package com.coding.service.impl;

import com.coding.base.pojo.BaseModel;
import com.coding.base.pojo.PageQueryModel;
import com.coding.base.util.DateUtil;
import com.coding.dao.GroupDao;
import com.coding.dao.GroupwithDao;
import com.coding.dao.UserDao;
import com.coding.pojo.*;
import com.coding.service.GroupwithService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Groupwith)表服务实现类
 *
 * @author makejava
 * @since 2020-07-31 14:33:09
 */
@Service
public class GroupwithServiceImpl implements GroupwithService {
    @Autowired
    GroupwithDao groupwithDao;
    @Autowired
    UserDao userDao;
    @Autowired
    GroupDao groupDao;
    @Override
    public void queryallbyuser_id(int user_id, PageQueryModel pageQueryModel, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        PageHelper.startPage(pageQueryModel.getPage(), pageQueryModel.getSize());
        Groupwith groupwith=new Groupwith();
        groupwith.setUserId(user_id);
        List<Groupwith> groupwithList=groupwithDao.queryAllWithImage(groupwith);
        PageInfo pageInfo = new PageInfo(groupwithList);
        groupwithList = pageInfo.getList();
        if (groupwithList.size() == 0) {
            if (pageInfo.getPageNum() > 1) {
                baseModel.setFailure("已经没有下一页了");
            } else {
                baseModel.setOther(2, "没有符合条件的班级");
            }
            return;
        }
        baseModel.setSucceed("查询成功，符合条件的班级共" + groupwithList.size() + "个", groupwithList);
    }

    @Override
    public void joingroup(int user_id, int group_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Group group=groupDao.queryById(group_id);
        if (group == null) {
            baseModel.setFailure("该圈子不存在，请检查账号是否正确");
            return;
        }
        Groupwith newgroupwith=groupwithDao.queryById(user_id,group_id);
        if (newgroupwith!=null){
            baseModel.setFailure("该用户已加入该圈子");
            return;
        }
        Groupwith groupwith=new Groupwith();
        groupwith.setUserId(user_id);
        groupwith.setGroupId(group_id);
        groupwith.setGroupTime(DateUtil.getStringDate());
        if (groupwithDao.insertSelective(groupwith)==1){
            baseModel.setSucceed("用户"+user_id+"成功加入圈子"+group_id);
        }else {
            baseModel.setFailure("用户加入圈子失败");
        }
    }

    @Override
    public void checkUserGroup(int user_id, int group_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Group group=groupDao.queryById(group_id);
        if (group == null) {
            baseModel.setFailure("该圈子不存在，请检查账号是否正确");
            return;
        }
        Groupwith newgroupwith=groupwithDao.queryById(user_id,group_id);
        if (newgroupwith==null){
            baseModel.setFailure("该用户尚未加入该圈子");
            return;
        }
        baseModel.setSucceed("改用户已加入该圈子",true);
    }

    @Override
    public void quitgroup(int user_id, int group_id, BaseModel baseModel) throws Exception {
        User user = userDao.getuserbyuser_id(user_id);
        if (user == null) {
            baseModel.setFailure("该用户不存在，请检查账号是否正确");
            return;
        }
        Group group=groupDao.queryById(group_id);
        if (group == null) {
            baseModel.setFailure("该圈子不存在，请检查账号是否正确");
            return;
        }
        Groupwith newgroupwith=groupwithDao.queryById(user_id,group_id);
        if (newgroupwith==null){
            baseModel.setFailure("该用户尚未加入该圈子");
            return;
        }
        Groupwith groupwith=new Groupwith();
        groupwith.setUserId(user_id);
        groupwith.setGroupId(group_id);
        groupwith.setGroupTime(DateUtil.getStringDate());
        if (groupwithDao.deleteById(user_id,group_id)==1){
            baseModel.setSucceed("用户"+user_id+"成功退出圈子"+group_id);
        }else {
            baseModel.setFailure("用户退出圈子失败");
        }
    }
}